package com.alan.wifishare.presenter;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.AreaInfo;
import com.alan.libapi.model.business.SharedWifiInfo;
import com.alan.libapi.model.business.UserBoundDevice;
import com.alan.libapi.model.business.UserInfo;
import com.alan.libapi.model.business.UserPackageSituation;
import com.alan.libapi.model.persistent.ErrorSets;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVMainConnectFM;

import java.util.List;

/**
 * @className: MainConnectPS
 * @describtion: MainConnectPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 18:43
 */
public class MainConnectPS {

    private IVMainConnectFM ivMainConnectFM;
    private boolean isConnectedLink = false;
    private int areaId = -1;
    private boolean isUserPackageValid = false;

    private MainConnectPS() {
    }

    public MainConnectPS(IVMainConnectFM ivMainConnectFM) {
        this.ivMainConnectFM = ivMainConnectFM;
        isUserPackageValid = false;
    }

    public void setIvMainConnectFM(IVMainConnectFM ivMainConnectFM) {
        this.ivMainConnectFM = ivMainConnectFM;
    }

    public boolean isConnectedLink() {
        return isConnectedLink;
    }

    public void getAreaIdByAddress(String country, String province, String city, String district){
        //test start
        KLog.e();
//        boolean tBug = true;
//        if(tBug){
//            areaId = 518057;
//            ivMainConnectFM.getAreaIdSuccess(areaId);
//            return;
//        }
        //test end

        GlobalField.businessHttp.getAreaIdByAddress(GlobalField.userSession, country, province, city, district, new CallbackListener<AreaInfo>() {
            @Override
            public void onSuccess(AreaInfo result) {
                areaId = result.getAreaId();
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainConnectFM.getAreaIdSuccess(areaId);
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                KLog.e(msg);
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainConnectFM.toastMsg(msg);
                    }
                });
            }
        });
    }

    /**
     * 刷新附近的wifi列表
     */
    public void refreshNearbyWifiList(int areaId, double longitude, double latitude) {
        KLog.e();
        ivMainConnectFM.showProgress();
        GlobalField.businessHttp.getNearbySharedWifiList(GlobalField.userSession, areaId, longitude, latitude, new CallbackListener<List<SharedWifiInfo>>() {
            @Override
            public void onSuccess(final List<SharedWifiInfo> result) {
                if(result != null){
                    GlobalField.sharedWifiInfoList = result;
                    //通知界面数据改变
                    GlobalField.handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ivMainConnectFM.hideProgress();
                            ivMainConnectFM.updateNearbyWifiListView(result);
                        }
                    });
                }
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainConnectFM.hideProgress();
                        ivMainConnectFM.toastMsg(msg);
                    }
                });
            }
        });
    }

    public void clickToConnectLink() {
        getUserBoundDevice();
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo(){
        KLog.e();
        GlobalField.businessHttp.getUserInfo(GlobalField.userSession, new CallbackListener<UserInfo>() {
            @Override
            public void onSuccess(UserInfo result) {
                if(result != null){
                    GlobalField.userInfo = result;
                }
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                KLog.e(msg);
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainConnectFM.toastMsg(msg);
                    }
                });
            }
        });
    }

    /**
     * 获取用户绑定的设备
     */
    public void getUserBoundDevice() {
        KLog.e();
        if(GlobalField.userBoundDevice != null){
            getUserPackageSituation();
            return;
        }

        GlobalField.businessHttp.getUserBoundDevice(GlobalField.userSession, new CallbackListener<UserBoundDevice>() {
            @Override
            public void onSuccess(final UserBoundDevice result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(result.getDeviceId() > 0){
                            GlobalField.userBoundDevice = result;
                            //如果已绑定设备，则获取用户当前套餐使用情况
                            //ivMainConnectFM.userHaveBoundDevice(result);
                            getUserPackageSituation();
                        }else {
                            ivMainConnectFM.gotoBindDeviceView();//没有绑定设备则跳转到设备绑定界面
                        }
                    }
                });
            }

            @Override
            public void onFailure(final int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(errorCode == ErrorSets.CODE_GET_BOUND_DEVICE_UNBOUND){
                            ivMainConnectFM.gotoBindDeviceView();//没有绑定设备则跳转到设备绑定界面
                        }
                        ivMainConnectFM.toastMsg(msg);
                    }
                });
            }
        });
    }

    /**
     * 获取用户
     */
    public void getUserPackageSituation() {
        KLog.e();
//        if(isDebug){
//            //当前套餐可以继续使用，进入连接界面
//            ivMainConnectFM.gotoConnectDeviceView();
//            return;
//        }
//        if(isUserPackageValid){
//            ivMainConnectFM.gotoConnectDeviceView();
//            return;
//        }
        GlobalField.businessHttp.getUserPackageSituation(GlobalField.userSession, new CallbackListener<UserPackageSituation>() {
            @Override
            public void onSuccess(final UserPackageSituation result) {
                // TODO: Alan 判断用户套餐情况是否可用
                KLog.e(result);
                KLog.e(System.currentTimeMillis());
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(System.currentTimeMillis()/1000 < result.getDeadline()){
                            if(result.getRemainingFlows() > 0 || result.getRemainingFlows() == -1){
                                //当前套餐可以继续使用，进入连接界面
                                isUserPackageValid = true;
                                ivMainConnectFM.gotoConnectDeviceView();
                            }else {
                                //当前套餐流量已使用完
                                ivMainConnectFM.toastMsg("当前套餐流量已使用完");
                                ivMainConnectFM.gotoBuyPackageView();
                            }
                        }else {
                            //当前套餐已过期
                            ivMainConnectFM.toastMsg("当前套餐已过期");
                            ivMainConnectFM.gotoBuyPackageView();
                        }
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ivMainConnectFM.toastMsg(msg);
                    }
                });
            }
        });
    }


//    public void connectLinkWifi(){
//        // TODO: Alan 扫描附近wifi通过mac地址匹配绑定的Link设备，如果有则连接，连接失败跳转至Link连接界面
//    }
}
